package com.example.user.player;

import  android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;

public class MusicPlayerService extends Service implements  MediaPlayer.OnPreparedListener{//implements Runnable {
    private MediaPlayer musicPlayer=null; // 媒体播放器对象
    private PlayerBinder mBinder;
    public  MusicPlayerService(){

    }
    public void onCreate(){
        super.onCreate();
        if(musicPlayer==null) {
            musicPlayer = new MediaPlayer();
        }
        musicPlayer.setOnPreparedListener(this);
    }
    public IBinder onBind(Intent intent) {
        mBinder=new PlayerBinder();

        return  mBinder;
       // return  new mBinder();
    }
   /* public class mBinder extends android.os.Binder{
        public void setData(String data){//③写一个公共方法，用来对data数据赋值。
            MusicPlayerService.this.path = data;
        }
    }*/
   public void onPrepared(MediaPlayer mp) {
       musicPlayer.start();
   }

    public class PlayerBinder extends Binder {

        public void play(String url) {
            try{     musicPlayer.reset();
                musicPlayer.setDataSource(url);
                musicPlayer.setLooping(false);

                musicPlayer.prepare();


            }catch  (IOException e) {
                e.printStackTrace();
            }
        }

        public void pause() {
            if (musicPlayer.isPlaying()) {
                musicPlayer.pause();
            }
        }

        public void resume() {

                musicPlayer.start();
           
        }
        public void release() {
            musicPlayer.release();
        }


    }





}
